﻿Imports Microsoft.VisualBasic.ControlChars

Public Class frmTabDistributors
    Public Shared cDistrib As New Collection 'the distributors of the data set
    Public Shared cStdOrderProc As New Collection 'standard order process descriptions
    Public Shared cDigitalForm As New Collection 'Digital forms for the standard order processes
    Public Shared cDTransOpt As New Collection 'Digital Transfer Options for Digital Forms
    Dim editNode As EditXML = New EditXML()
    Public Shared sPdelim As String = "£"
    Public Shared sKdelim As String = CrLf

    Private Sub btnAddDistrib_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddDistrib.Click
        Dim s1 As String = ""
        Dim iCount As Integer = 0
        Dim oNewDistrib As New DistributorClass
        Dim oNewContact As New ContactInfo
        Dim frmNewDist As New frmDistribution(iCount)
        frmNewDist.ShowDialog()
        If (frmNewDist.bUpdate = True) Then
            oNewDistrib = frmNewDist.oCurDistrib
            cDistrib.Add(frmNewDist.oCurDistrib) 'add new distributor to main collection
            'identify distributor in list box using required contact information
            oNewContact = oNewDistrib.Distributor
            If (oNewContact.PrimaryPerson = True) Then
                s1 = "Distributor, person: " & oNewContact.Person
            Else
                s1 = "Distributor, organization: " & oNewContact.Organization
            End If
            lboxDistrib.Items.Add(s1)
        End If
    End Sub

    Private Sub btnEditDistrib_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEditDistrib.Click
        Dim iCount As Integer = lboxDistrib.Items.Count
        Dim indexVal As Integer = lboxDistrib.SelectedIndex
        Dim s1 As String = ""
        If (indexVal = -1) Then
            If (iCount = 0) Then
                s1 = "No Distributor available to edit."
                MessageBox.Show(s1, "Selection error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                Return
            ElseIf (iCount = 1) Then
                indexVal = 0
            Else
                s1 = "No Distributor chosen for editing."
                MessageBox.Show(s1, "Selection error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                Return
            End If
        End If

        Dim oEditDistrib As New DistributorClass
        Dim oEditContact As New ContactInfo
        Dim frmEditDist As New frmDistribution(indexVal + 1) 'convert from 0-based list to 1-based collection
        frmEditDist.ShowDialog()
        If (frmEditDist.bUpdate = True) Then
            oEditDistrib = frmEditDist.oCurDistrib
            cDistrib.Add(oEditDistrib, , indexVal + 1) 'insert edited distributor into main collection
            cDistrib.Remove(indexVal + 2) 'remove old version; 1-based index
            'update distributor identification in list box using required contact information
            oEditContact = oEditDistrib.Distributor
            If (oEditContact.PrimaryPerson = True) Then
                s1 = "Distributor, person: " & oEditContact.Person
            Else
                s1 = "Distributor, organization: " & oEditContact.Organization
            End If
            lboxDistrib.Items.Insert(indexVal, s1) 'insert edited identifier into list box
            lboxDistrib.Items.RemoveAt(indexVal + 1) 'remove old version; 0-based index
        End If
        btnEditDistrib.Enabled = False
        btnDelDistrib.Enabled = False
    End Sub

    Private Sub btnDelDistrib_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelDistrib.Click
        Dim indexVal As Integer = lboxDistrib.SelectedIndex
        Dim s1 As String = ""
        If (indexVal = -1) Then
            s1 = "No Distributor chosen for deletion."
            MessageBox.Show(s1, "Selection error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return
        End If

        cDistrib.Remove(indexVal + 1) '1-based collection of Distributors
        lboxDistrib.Items.RemoveAt(indexVal) '0-based collection in list box
        btnEditDistrib.Enabled = False
        btnDelDistrib.Enabled = False
    End Sub

    Private Sub lboxDistrib_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lboxDistrib.SelectedIndexChanged
        If lboxDistrib.SelectedItems.Count() > 0 Then
            btnEditDistrib.Enabled = True
            btnDelDistrib.Enabled = True
        Else
            btnEditDistrib.Enabled = False
            btnDelDistrib.Enabled = False
        End If
    End Sub

    Private Sub frmTabDistributors_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Leave
        editXMLFile()
    End Sub
    Public Sub editXMLFile()
        editNode.Remove("/metadata", "distinfo")
        Dim newNode As String = ""
        For i As Integer = cDistrib.Count() To 1 Step -1
            Dim item As DistributorClass = cDistrib(i)
            newNode = "<distrib>"
            newNode = newNode & editNode.createContactInfo(item.Distributor)
            newNode = newNode & "</distrib>"

            newNode = newNode & "<resdesc>" & item.ResourceDesc & "</resdesc>"
            newNode = newNode & "<distliab>" & item.Liability & "</distliab>"

            For j As Integer = 1 To item.StdOrderColl.Count()
                Dim item2 As StdOrderClass = cStdOrderProc(item.StdOrderColl(j))
                newNode = newNode & "<stdorder>"
                If item2.NonDigitalForm <> "" Then
                    newNode = newNode & "<nondig>" & item2.NonDigitalForm & "</nondig>"
                Else
                    For k As Integer = 1 To item2.DigitalForms.Count()
                        Dim digiform As DigitalOrderClass = cDigitalForm(item2.DigitalForms(k)) 'item2.DigitalForms(k)
                        newNode = newNode & "<digform>"
                        newNode = newNode & "<digtinfo>"
                        newNode = newNode & "<formname>" & digiform.FormatName & "</formname>"
                        If digiform.FormatVNum <> "" Then
                            newNode = newNode & "<formvern>" & digiform.FormatVNum & "</formvern>"
                        ElseIf digiform.FormatVDate <> "" Then
                            newNode = newNode & "<formverd>" & digiform.FormatVDate & "</formverd>"
                        End If
                        newNode = newNode & "<formspec>" & digiform.FormatSpec & "</formspec>"
                        newNode = newNode & "<formcont>" & digiform.FormatInfo & "</formcont>"
                        newNode = newNode & "<filedec>" & digiform.Decompress & "</filedec>"
                        newNode = newNode & "<transize>" & digiform.TransferSize & "</transize>"

                        newNode = newNode & "<asciistr>"
                        newNode = newNode & "<recdel>" & digiform.RecordDelim & "</recdel>"
                        newNode = newNode & "<numheadl>" & digiform.NumHeaders & "</numheadl>"
                        newNode = newNode & "<deschead>" & digiform.Header & "</deschead>"
                        newNode = newNode & "<orienta>" & digiform.Orientation & "</orienta>"
                        newNode = newNode & "<casesens>" & digiform.CaseSensitive & "</casesens>"
                        newNode = newNode & "<authent>" & digiform.Authentic & "</authent>"
                        newNode = newNode & "<quotech>" & digiform.QuoteChar & "</quotech>"
                        For m As Integer = 1 To digiform.DataFields.Count()
                            Dim fields() As String = digiform.DataFields(m - 1).Split(sPdelim)
                            newNode = newNode & "<datafiel>"
                            newNode = newNode & "<dfieldnm>" & fields(0) & "</dfieldnm>"
                            newNode = newNode & "<missingv>" & fields(1) & "</missingv>"
                            If fields(2) = "delim" Then
                                newNode = newNode & "<dfwidthd>" & fields(3) & "</dfwidthd>"
                            ElseIf fields(2) = "width" Then
                                newNode = newNode & "<dfwidth>" & fields(3) & "</dfwidth>"
                            End If
                            newNode = newNode & "</datafiel>"
                        Next
                        newNode = newNode & "</asciistr>"

                        newNode = newNode & "</digtinfo>"
                        For m As Integer = 1 To digiform.TransOptions.Count()
                            Dim opt As DTransOption = cDTransOpt(digiform.TransOptions(m))
                            newNode = newNode & "<digtopt>"
                            If opt.boolOnline Then
                                newNode = newNode & "<onlinopt>"
                                newNode = newNode & "<computer><networka><networkr>" & opt.NetworkName & "</networkr></networka></computer>"
                                newNode = newNode & "<accinstr>" & opt.Access & "</accinstr>"
                                newNode = newNode & "<oncomp>" & opt.ComputerOS & "</oncomp>"
                                newNode = newNode & "</onlinopt>"
                            ElseIf opt.boolOffline Then
                                newNode = newNode & "<offoptn>"
                                newNode = newNode & "<offmedia>" & opt.OfflineMedia & "</offmedia>"
                                newNode = newNode & "<reccap><recden>" & opt.RecDensity & "</recden><recdenu>" & opt.DensityUnits & "</recdenu></reccap>"
                                Dim formats() As String = opt.RecFormat.Split(sKdelim)
                                For n As Integer = 1 To formats.GetLength(0)
                                    newNode = newNode & "<recfmt>" & formats(n - 1).Trim() & "</recfmt>"
                                Next
                                newNode = newNode & "<compat>" & opt.Compatible & "</compat>"
                                newNode = newNode & "</offoptn>"
                            End If
                            newNode = newNode & "</digtopt>"
                        Next
                        newNode = newNode & "</digform>"
                    Next
                End If
                newNode = newNode & "<fees>" & item2.Fees & "</fees>"
                newNode = newNode & "<ordering>" & item2.Instructions & "</ordering>"
                newNode = newNode & "<turnarnd>" & item2.Turnaround & "</turnarnd>"
                newNode = newNode & "</stdorder>"
            Next

            newNode = newNode & "<custom>" & item.CustomOrder & "</custom>"
            newNode = newNode & "<techpreq>" & item.TechReq & "</techpreq>"

            newNode = newNode & "<availabl>"
            newNode = newNode & editNode.createTimeInfo(item.TimeAvail, item.boolSingle, item.boolRange)
            newNode = newNode & "</availabl>"
            editNode.Add("/metadata", "/eainfo", "distinfo", newNode)
        Next
    End Sub
    Public Function getDTransOpt() As Collection
        Return cDTransOpt
    End Function
    Public Function getDigitalForm() As Collection
        Return cDigitalForm
    End Function
    Public Sub addDTransOpt(ByVal newitem As DTransOption)
        cDTransOpt.Add(newitem)
    End Sub
    Public Sub addDTransOpt(ByVal newitem As DTransOption, ByVal newindex As Integer)
        cDTransOpt.Add(newitem, , newindex)
    End Sub
    Public Sub removeDTransOpt(ByVal index As Integer)
        cDTransOpt.Remove(index)
    End Sub
    Public Function getDistrib() As Collection
        Return cDistrib
    End Function
    Public Function getStdOrderProc() As Collection
        Return cStdOrderProc
    End Function
    Public Sub addStdOrderProc(ByVal newitem As StdOrderClass)
        cStdOrderProc.Add(newitem)
    End Sub
    Public Sub addStdOrderProc(ByVal newitem As StdOrderClass, ByVal newindex As Integer)
        cStdOrderProc.Add(newitem, , newindex)
    End Sub
    Public Sub removeStdOrderProc(ByVal index As Integer)
        cStdOrderProc.Remove(index)
    End Sub

    Private Sub button_example_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button_example.Click
        Dim helpForm As dialog_help = New dialog_help("Distributors")
        helpForm.ShowDialog()
    End Sub
End Class